package com.jade.logger;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.*;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.jface.action.*;
import org.eclipse.ui.*;
import org.eclipse.swt.SWT;

public class Logger extends ViewPart {

	/**
	 * The ID of the view as specified by the extension.
	 */
	public static final String ID = "com.jade.logger.Logger";
	public static String STATE_ERROR = "ERROR";
	public static String STATE_DELETE = "DELETE";
	public static String STATE_INFO = "INFO";
	public static String STATE_SUCCESS = "SUCCESS";
	public static String STATE_WARNING = "WARN";
	public static String STATE_EMPTY = "EMPTY";

	private static TableViewer viewer;
	private Action actionClear;
	
	private static final ImageDescriptor CLEAR = Activator.getImageDescriptor("icons/ICON_ERASER.png");	
	
	/**
	 * This method creates columns
	 * @param parent final Composite
	 * @param viwer final TableViewer
	 */
	private void createColumns(final Composite parent, final TableViewer viwer){
		String[] titles = {"Time", "Message"};
		int[] bounds = {70, 800};
		
		TableViewerColumn col = createTableViewerColumn(titles[0], bounds[0], 0);
		col.setLabelProvider(new ColumnLabelProvider(){
			
			public String getText(Object o){
				Message message = (Message) o;
				return message.getTime();
			}
			
		});
		
		col = createTableViewerColumn(titles[1], bounds[1], 1);
		col.setLabelProvider(new ColumnLabelProvider(){
			
			public String getText(Object o){
				Message message = (Message) o;
				return message.getText();
			}
			
			public Image getImage(Object o){
				Message message = (Message) o;
				return message.getImage();
			}
			
		});
	}
	
	
	/**
	 * This Method Creates current column with following parameters
	 * @param title Column title
	 * @param bound Column size
	 * @param colNumber Number of column
	 * @return Created TableViewerColumn
	 */
	private TableViewerColumn createTableViewerColumn(String title, int bound,
			final int colNumber) {
		final TableViewerColumn viewerColumn = new TableViewerColumn(viewer,
				SWT.NONE);
		final TableColumn column = viewerColumn.getColumn();
		column.setText(title);
		column.setWidth(bound);
		column.setResizable(true);
		column.setMoveable(true);
		return viewerColumn;
	}
	
	
	/**
	 * This Method creates action bars
	 */
	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
		fillLocalToolBar(bars.getToolBarManager());
	}

	
	/**
	 * This Method fills Local Tool Bar
	 * @param manager IToolBarManager where actions will be added
	 */
	private void fillLocalToolBar(IToolBarManager manager) {
		manager.add(actionClear);
	}

	
	/**
	 * This method initializes actions
	 */
	private void makeActions() {
		actionClear = new Action() {
			public void run() {
				
				ModelProvider.INSTANCE.clear();
				viewer.refresh();
			}
		};
		actionClear.setText("Clear");
		actionClear.setToolTipText("Clear Log");
		actionClear.setImageDescriptor(CLEAR);
		

		
	}
	
	
	/**
	 * This is a callback that will allow us
	 * to create the viewer and initialize it.
	 */
	public void createPartControl(Composite parent) {
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
		
		createColumns(parent, viewer);
		
		final Table table = viewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true); 
		
		viewer.setContentProvider(new ArrayContentProvider());
		
		viewer.setInput(ModelProvider.INSTANCE.getMessages());
		
		getSite().setSelectionProvider(viewer);
		
		PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "com.jade.logger.viewer");
		makeActions();
		contributeToActionBars();
	}
	

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		viewer.getControl().setFocus();
	}
	
	
	/**
	 * This method prints message into logger
	 * @param msgType type of message, use public static fields of this class
	 * @param msgText text of message
	 */
	public static void print(String msgType, String msgText){

		Message msg = new Message(msgType, msgText);
		ModelProvider.INSTANCE.addMessage(msg);
		viewer.refresh();
		
		viewer.getTable().showItem(viewer.getTable().getItem(viewer.getTable().getItemCount()-1)); 
		
	}
	
}